package com.shuoyue.ycgk.ui.shop.address;

import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.base.BaseMvpActivity;
import com.shuoyue.ycgk.entity.address.AddressBean;
import com.shuoyue.ycgk.entity.address.OrderAddressBean;
import com.shuoyue.ycgk.ui.system.SystemAddressContract;
import com.shuoyue.ycgk.utils.EditCheckUtil;
import com.shuoyue.ycgk.utils.EmojiFilter;
import com.shuoyue.ycgk.utils.XToast;
import com.shuoyue.ycgk.views.AddressPickTask;
import com.shuoyue.ycgk.views.dialog.addresspicker.AddressPickerDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.addapp.pickers.entity.City;
import cn.addapp.pickers.entity.County;
import cn.addapp.pickers.entity.Province;
import cn.addapp.pickers.picker.AddressPicker;
import me.jessyan.autosize.internal.CancelAdapt;

public class EditAddressActivity extends BaseMvpActivity<SelectAddressContract.Presenter>
        implements SelectAddressContract.View, CancelAdapt,
        SystemAddressContract.View {


    @BindView(R.id.page_title)
    TextView pageTitle;
    @BindView(R.id.name)
    EditText name;
    @BindView(R.id.pnoneNum)
    EditText pnoneNum;
    @BindView(R.id.address)
    TextView address;
    @BindView(R.id.addressDetail)
    EditText addressDetail;
    @BindView(R.id.isdefault)
    CheckBox isdefault;
    OrderAddressBean orderAddressBean;

    Province province;
    City city;
    County area;

    SystemAddressContract.Presenter systemAddressPresenter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_add_address;
    }

    @Override
    protected void initView() {
        pageTitle.setText("编辑地址");
    }

    @Override
    public void initData() {
        mPresenter = new SelectAddressContract.Presenter();
        mPresenter.attachView(this);
        systemAddressPresenter = new SystemAddressContract.Presenter();
        systemAddressPresenter.attachView(this);
        orderAddressBean = (OrderAddressBean) getIntent().getSerializableExtra("add");
        if (orderAddressBean != null) {
            name.setText(orderAddressBean.getContact());
            pnoneNum.setText(orderAddressBean.getPhone());
            address.setText(orderAddressBean.getProvinceName() + "/" + orderAddressBean.getCityName() + "/" + orderAddressBean.getRegionName());
            addressDetail.setText(orderAddressBean.getAddress());
            isdefault.setChecked(orderAddressBean.getIsDefault() == 1);
            province = new Province();
            province.setAreaId(orderAddressBean.getProvinceId() + "");
            province.setAreaName(orderAddressBean.getProvinceName());
            city = new City();
            city.setAreaId(orderAddressBean.getCityId() + "");
            city.setAreaName(orderAddressBean.getCityName());
            area = new County();
            area.setAreaId(orderAddressBean.getRegionId() + "");
            area.setAreaName(orderAddressBean.getRegionName());
        }
    }

    @Override
    public void setAddress(List<OrderAddressBean> data) {

    }

    @Override
    public void editSuc() {
        finish();
    }

    @Override
    public void addSuc() {
        finish();
    }

    @Override
    public void deleteSuc() {

    }


    @OnClick({R.id.back, R.id.isdefault, R.id.submit, R.id.address})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.isdefault:

                break;
            case R.id.submit:
                submit();
                break;
            case R.id.address:
//                showAddressPicker();
//                systemAddressPresenter.getArea(0, 1);
                if (addressPicker != null) {
                    addressPicker.show();
                } else {
                    systemAddressPresenter.getAllArea(mContext);
                }
                break;
        }
    }

    void submit() {
        if (!EditCheckUtil.isTelPhoneNumber(pnoneNum.getText().toString())) {
            toast("请输入正确的手机号码");
            return;
        }
        if (province == null || city == null || area == null) {
            toast("请选择地区");
            return;
        }
        if (addressDetail.getText().toString().length() == 0) {
            toast("请输入详细地址");
            return;
        }
        if (TextUtils.isEmpty(name.getText().toString())) {
            toast("请输入真实姓名");
            return;
        }
        if (EmojiFilter.containsEmoji(addressDetail.getText().toString())) {
            toast("地址不能包含表情符号");
            return;
        }

        if (orderAddressBean == null) {
            orderAddressBean = new OrderAddressBean();
        }
        if (province != null) {
            orderAddressBean.setProvinceName(province.getAreaName());
            orderAddressBean.setProvinceId(Integer.valueOf(province.getAreaId()));
        }

        if (city != null) {
            orderAddressBean.setCityName(city.getAreaName());
            orderAddressBean.setCityId(Integer.valueOf(city.getAreaId()));
        }

        if (area != null) {
            orderAddressBean.setRegionName(area.getAreaName());
            orderAddressBean.setRegionId(Integer.valueOf(area.getAreaId()));
        }

        orderAddressBean.setContact(name.getText().toString());
        orderAddressBean.setPhone(pnoneNum.getText().toString());
        orderAddressBean.setAddress(addressDetail.getText().toString());
        orderAddressBean.setIsDefault(isdefault.isChecked() ? 1 : 0);
        mPresenter.edit(orderAddressBean);
    }


    @Override
    public void setAreas(List<AddressBean> areas, int pid, int desLevel) {
    }

    @Override
    public void setAreas(ArrayList<Province> provinces) {
        showAddressPicker(provinces);
    }


    AddressPicker addressPicker;

    void showAddressPicker(ArrayList<Province> result) {
        addressPicker = new AddressPicker(EditAddressActivity.this, result);
        addressPicker.setCanLoop(true);
        addressPicker.setColumnWeight(2 / 8.0f, 3 / 8.0f, 3 / 8.0f);//省级、地级和县级的比例为2:3:3
        addressPicker.setOnLinkageListener(new AddressPickTask.Callback() {
            @Override
            public void onAddressInitFailed() {

            }

            @Override
            public void onAddressPicked(Province var1, City var2, County var3) {
                province = var1;
                city = var2;
                area = var3;
                address.setText(var1.getAreaName() + "/" + var2.getAreaName() + "/" + var3.getAreaName());
            }
        });
        addressPicker.show();
    }

}
